<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @change="getGoodsList()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="getAddPages()">添加按钮</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodsList" style="width: 100%">
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="220px">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="100px">
        </el-table-column>
        <el-table-column prop="goods_number" label="商品数量" width="100px">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100px">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="220px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope" class="operation_col">
            <el-button type="primary" size="mini" @click="editGoods(scope.row)"
              ><i class="el-icon-edit"></i
            ></el-button>
            <el-button
              type="primary"
              size="mini"
              @click="deleteGoods(scope.row)"
              ><i class="el-icon-delete"></i
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
    data() {
        return {
            goodsList:[],
            queryInfo:{
              'query':'',
              'pagenum':1,
              'pagesize':5
            },
            total:0,       
        }
    },
    created() {
        this.getGoodsList()
        
    },
    methods: {
        async getGoodsList(){
          const{data:res} = await this.$http.get('goods', {params:this.queryInfo})
          if(res.meta.status!==200){
              this.$message.error(res.meta.msg)
              return
          }
          this.goodsList = res.data.goods
          this.total =res.data.total
          this.queryInfo.query=''
        },
        handleSizeChange(val) {
           this.queryInfo.pagesize=val
           this.getGoodsList()
        },
        handleCurrentChange(val) {
            this.queryInfo.pagenum=val
            this.getGoodsList()
        },
        editGoods(row){
            console.log(row)
            
        },
        deleteGoods(row){
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(async () => {
                const {data:res} = await this.$http.delete('goods/'+row.goods_id)
                if(res.meta.status!==200){
                    this.$message.error('删除失败')
                    return
                }
                    this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
                this.getGoodsList()
            
                }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
                

        },

        getAddPages(){
            this.$router.push('/goods/add')
        }
        
    },

}
</script>

<style scoped>
.operation_col {
  width: 120px;
}
.el-table {
  text-align: center;
}
</style>